package ch.usi.inf.pf2.spreadsheet;

/**
 * @author Ehsanvala.uk@gmail.com
*/


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Action;

import javax.swing.CellEditor;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.UndoableEditListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import ch.usi.inf.pf2.cell.Cell;


public final class JSpreadsheet extends JComponent {

	private static final int COL_WIDTH = 60;
	private static final int ROW_HEIGHT = 17;
	private static final Color BACKGROUND_COLOR = Color.WHITE;
	private static final Color GRID_COLOR = new Color(200, 200, 250);
	private static final Color SELECTION_COLOR = new Color(230, 230, 255);
	private static final Color FOCUS_COLOR = new Color(170, 170, 240);
	private static final Color TEXT_COLOR = Color.BLACK;

	private final SpreadSheetCellArray model;
	private boolean hasFocus;
	private int selectedRow;
	private int selectedCol;

	public JSpreadsheet(final SpreadSheetCellArray model) {
		this.model = model;
		model.addSpreadSheetListener(new SpreadSheetCellArrayListener() {
			public void spreadSheetChanged() {
				repaint();
			}
		});


		addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
				System.out.println(e);
			}
			public void mousePressed(MouseEvent e) {
				System.out.println(e);
				requestFocus();
			}
			public void mouseExited(MouseEvent e) {
				System.out.println(e);
			}
			public void mouseEntered(MouseEvent e) {
				System.out.println(e);
			}
			public void mouseClicked(MouseEvent e) {
				System.out.println(e);
			}
		});
		addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {
				System.out.println(e);
				final char text = e.getKeyChar();
				model.setValueAt(""+text, selectedCol, selectedRow);
			}
			public void keyReleased(KeyEvent e) {
				System.out.println(e);
			}
			public void keyPressed(KeyEvent e) {
				System.out.println(e);
				if (e.getKeyCode()==KeyEvent.VK_DOWN) {
					selectedRow++;
				} else if (e.getKeyCode()==KeyEvent.VK_UP) {
					selectedRow--;
				}
				else if(e.getKeyCode()==KeyEvent.VK_LEFT){
					selectedCol--;

				}
				else if (e.getKeyCode()==KeyEvent.VK_RIGHT){
					selectedCol++;
				}
				repaint();
			}
		});


		addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
				System.out.println(e);
				hasFocus = false;
				repaint();


			}
			public void focusGained(FocusEvent e) {
				System.out.println(e);
				hasFocus = true;
				repaint();
			}
			
		});
	}
		
		public Dimension getPreferredSize() {
			return new Dimension(model.getColumnCount()*COL_WIDTH, model.getRowCount()*ROW_HEIGHT);
		}
		
		protected void paintComponent(Graphics g) {
			System.out.println("painting");
			// background
			g.setColor(BACKGROUND_COLOR);
			g.fillRect(0, 0, getWidth(), getHeight());
			
			// selection
			g.setColor(SELECTION_COLOR);
			g.fillRect(selectedCol*COL_WIDTH, selectedRow*ROW_HEIGHT, COL_WIDTH, ROW_HEIGHT);
			
			// grid
			g.setColor(GRID_COLOR);
			for (int col = 0; col < model.getColumnCount(); col++) {
				g.drawLine(col*COL_WIDTH, 0, col*COL_WIDTH, getHeight());			
			}
			for (int row = 0; row < model.getRowCount(); row++) {
				g.drawLine(0, row*ROW_HEIGHT, getWidth(), row*ROW_HEIGHT);
			}
			
			// contents
			g.setColor(TEXT_COLOR);
			final FontMetrics fm = getFontMetrics(getFont());
			for (int row=0; row<model.getRowCount(); row++) {
				for (int col = 0; col < model.getColumnCount(); col++) {
					g.drawString(Cell.getStr(), col*COL_WIDTH, row*ROW_HEIGHT+fm.getAscent());
				}			
			}
			
			// focus
			if (hasFocus) {
				g.setColor(FOCUS_COLOR);
				g.drawRect(0, 0, getWidth()-1, getHeight()-1);
			}

		}
}